package com.yunxiang.summer.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.yunxiang.summer.R;
import com.yunxiang.summer.domain.TimeLineItem;

import java.util.List;

public class TimeLineAdapter extends RecyclerView.Adapter<TimeLineAdapter.TimeLineViewHolder> {

    private List<TimeLineItem> mDataList;

    public TimeLineAdapter(List<TimeLineItem> dataList) {
        this.mDataList = dataList;
    }

    @NonNull
    @Override
    public TimeLineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_time_line, parent, false);
        return new TimeLineViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TimeLineViewHolder holder, int position) {
        TimeLineItem item = mDataList.get(position);

        // 设置日期和时间
        holder.tvDate.setText(item.getDate());
        holder.tvTime.setText(item.getTime());
        holder.tvContent.setText(item.getContent());

        // 显示图片（如有）
        if (item.getPhotoRes() != -1) {
            holder.ivPhoto.setVisibility(View.VISIBLE);
            holder.ivPhoto.setImageResource(item.getPhotoRes());
        } else {
            holder.ivPhoto.setVisibility(View.GONE);
        }

        // 控制连接线显示（第一个条目隐藏顶部线，最后一个隐藏底部线）
        holder.viewTopLine.setVisibility(item.isShowTopLine() ? View.VISIBLE : View.INVISIBLE);
        holder.viewBottomLine.setVisibility(item.isShowBottomLine() ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    // ViewHolder
    public static class TimeLineViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate, tvTime, tvContent;
        ImageView ivPhoto;
        View viewTopLine, viewBottomLine, viewDot;

        public TimeLineViewHolder(@NonNull View itemView) {
            super(itemView);
            // 绑定控件
            tvDate = itemView.findViewById(R.id.tv_date);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvContent = itemView.findViewById(R.id.tv_content);
            ivPhoto = itemView.findViewById(R.id.iv_photo);
            viewTopLine = itemView.findViewById(R.id.view_top_line);
            viewBottomLine = itemView.findViewById(R.id.view_bottom_line);
            viewDot = itemView.findViewById(R.id.view_dot);
        }
    }
}